package algorithm;

import scicalc.Calculate;

import java.util.List;

public enum Linkage {
    SINGLE_LINKAGE(0), COMPLETE_LINKAGE(1), AVERAGE_LINKAGE(2);

    private int code;

    Linkage(int code) {
        this.code = code;
    }

    public double distance(double[][] X, List<Integer> a, List<Integer> b) {
        double dist = 0;
        double[][] Xa = Calculate.choose(X, a);
        double[][] Xb = Calculate.choose(X, b);
        double[][] distanceAB = new double[a.size()][b.size()];
        for (int i = 0; i < a.size(); i++) {
            distanceAB[i] = Calculate.sqrt(Calculate.sum(Calculate.pow(Calculate.minus(Xb, Xa[i]), 2), Calculate.AXIS_1));
        }
        switch (this.code) {
            case 0:
                double min = Double.MAX_VALUE;
                for (double[] line : distanceAB)
                    for (double d : line)
                        min = d < min ? d : min;
                dist = min;
                break;
            case 1:
                double max = 0;
                for (double[] line : distanceAB)
                    for (double d : line)
                        max = d > max ? d : max;
                dist = max;
                break;
            case 2:
                dist = Calculate.sum(distanceAB) / a.size() / b.size();
                break;
        }
        return dist;
    }
}
